package com.submit.demo.service.impl;

import com.csvreader.CsvReader;
import com.submit.demo.common.api.CommonPage;
import com.submit.demo.model.ColTag;
import com.submit.demo.model.Collector;
import com.submit.demo.repository.ColTagRepository;
import com.submit.demo.repository.CollectorRepository;
import com.submit.demo.service.CollectorService;
import com.submit.demo.util.MongoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CollectorServiceImpl implements CollectorService {
    @Autowired
    private CollectorRepository collectorRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoUtil mongoUtil;

    @Autowired
    private ColTagRepository colTagRepository;

    @Override
    public boolean isCollectorExist(String phone) {
        if(collectorRepository.findCollectorByPhone(phone) != null) return true;
        return false;
    }

    @Override
    public Integer createCollector(Collector collector) {
        if(collectorRepository.findCollectorByPhone(collector.getPhone()) != null) return 0;
        Collector c = collectorRepository.insert(collector);
        ColTag colTag = new ColTag();
        colTag.setCid(c.getId());
        List<String> tags = new ArrayList<>();
        tags.add("所有");
        colTag.setTags(tags);
        colTagRepository.insert(colTag);
        return 1;
    }

    @Override
    public Integer deleteCollector(String id) {
        collectorRepository.deleteById(id);
        return 1;
    }

    @Override
    public Integer insertFromCsv(MultipartFile file) {
        List<Collector> list = new ArrayList<>();
        try {
            CsvReader csvReader = new CsvReader(file.getInputStream(),',', Charset.forName("GBK"));
            csvReader.readHeaders();
            StringBuilder sb = new StringBuilder();
            while(csvReader.readRecord()){
                Collector collector = new Collector();
                String[] strs = csvReader.getValues();
                collector.setName(new String(strs[0].getBytes(), "UTF-8"));
                collector.setPhone(strs[1]);
                collector.setStatus(1);
                if(!isCollectorExist(collector.getPhone()) && !list.contains(collector)) list.add(collector);
                else sb.append(collector.getName() + ' ');
            }
            log.info("下列收集人已存在: " + sb.toString());
            List<Collector> collectors = collectorRepository.insert(list);
            for(Collector c : collectors){
                ColTag colTag = new ColTag();
                colTag.setCid(c.getId());
                List<String> tags = new ArrayList<>();
                tags.add("所有");
                colTag.setTags(tags);
                colTagRepository.insert(colTag);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return 1;
    }

    @Override
    public CommonPage<Collector> fetchAll(Integer currentPage, Integer pageSize) {
        Query query = new Query(new Criteria());
        long count = mongoTemplate.count(query, Collector.class);
        mongoUtil.start(currentPage, pageSize, query);
        List<Collector> list = mongoTemplate.find(query, Collector.class);
        CommonPage commonPage = mongoUtil.pageHelper(count, list);
        return commonPage;
    }
}
